package com.jy.app.lib.activity;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;

import com.jy.app.lib.R;
import com.jy.app.lib.common.StartActivityOkResult;
import com.jy.app.lib.common.StartActivityResult;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by 15014 on 2017/3/28.
 */

public class CommonFragmentActivity extends FragmentActivity {
    /**
     * 整个Activity视图的根视图
     */
    View decorView;

    /**
     * 手指按下时的坐标
     */
    float downX, downY;

    /**
     * 手机屏幕的宽度和高度
     */
    float screenWidth, screenHeight;

    private boolean isSideslipFinish = true;

    private Map<Integer,StartActivityResult> startPageResultMap = new HashMap<>();
    @Override
    public void setContentView(int layoutResID) {
        setTheme(R.style.translucent);
        super.setContentView(layoutResID);
        // 获得decorView
        decorView = getWindow().getDecorView();

        // 获得手机屏幕的宽度和高度，单位像素
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        screenWidth = metrics.widthPixels;
        screenHeight = metrics.heightPixels;
    }

    public void setIsSideslipFinish(boolean isSideslipFinish){
        this.isSideslipFinish = isSideslipFinish;
    }
    /**
     * 通过重写该方法，对触摸事件进行处理
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (isSideslipFinish)
        if(event.getAction() == MotionEvent.ACTION_DOWN){
            downX = event.getX();
            downY = event.getY();
        }else if(event.getAction() == MotionEvent.ACTION_MOVE){
            float moveDistanceX = event.getX() - downX;
            if(moveDistanceX > 0){
                decorView.setX(moveDistanceX);
            }
        }else if(event.getAction() == MotionEvent.ACTION_UP){
            float moveDistanceX = event.getX() - downX;
            if (moveDistanceX>0)
            if(moveDistanceX > screenWidth / 3){
                // 如果滑动的距离超过了手机屏幕的一半, 滑动处屏幕后再结束当前Activity
                continueMove(moveDistanceX);
            }else{
                // 如果滑动距离没有超过一半, 往回滑动
                rebackToLeft(moveDistanceX);
            }
        }
        return super.onTouchEvent(event);
    }

    /**
     * 从当前位置一直往右滑动到消失。
     * 这里使用了属性动画。
     */
    private void continueMove(float moveDistanceX){
        // 从当前位置移动到右侧。
        ValueAnimator anim = ValueAnimator.ofFloat(moveDistanceX, screenWidth);
        anim.setDuration(300); // 一秒的时间结束, 为了简单这里固定为1秒
        anim.start();

        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                // 位移
                float x = (float) (animation.getAnimatedValue());
                decorView.setX(x);
            }
        });

        // 动画结束时结束当前Activity
        anim.addListener(new AnimatorListenerAdapter() {

            @Override
            public void onAnimationEnd(Animator animation) {
                finish();
            }

        });
    }

    /**
     * Activity被滑动到中途时，滑回去~
     */
    private void rebackToLeft(float moveDistanceX){
        ObjectAnimator.ofFloat(decorView, "X", moveDistanceX, 0).setDuration(300).start();
    }

    public void startActivityForResult(Intent intent, int requestCode, StartActivityResult result){
        startPageResultMap.put(requestCode,result);
        startActivityForResult(intent,requestCode);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (startPageResultMap.size() != 0){
            StartActivityResult pageResult = startPageResultMap.get(requestCode);
            if (pageResult!=null){
                if (pageResult instanceof StartActivityOkResult){
                    if (resultCode == RESULT_OK){
                        pageResult.onResult(requestCode,resultCode,data);
                    }
                }else{
                    pageResult.onResult(requestCode,resultCode,data);
                }
                startPageResultMap.remove(requestCode);
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
